<?php
/**
 * Created by IntelliJ IDEA.
 * User: luwei
 * Date: 2017/6/24
 * Time: 22:31
 */

namespace app\modules\api\controllers;


use app\models\User;
use app\models\WechatApp;
use app\modules\api\models\LoginForm;
use Curl\Curl;

class PassportController extends Controller
{
    public function actionLogin()
    {
        $form = new LoginForm();
        $form->attributes = \Yii::$app->request->post();
        $form->wechat_app = $this->wechat_app;
        $form->store_id = $this->store->id;
        return $this->renderJson($form->login());
    }
    public function actionCode(){

        $request = \Yii::$app->request;
//        $appid = $request->post('appid');
        $store_id = $this->store->id;
        $res = WechatApp::find()->where(['id'=>$store_id])->asArray()->all();
        $appid=$res[0]['app_id'];
        $secret = $res[0]['app_secret'];
//        var_dump($secret);
//        exit();
//        $secret = $request->post('secret');
        $code = $request->get('code');
//        var_dump($res);
//        exit();
        $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
//       echo $api;
//       exit();
        $curl = new Curl();
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
        $curl->get($api);
        $res = $curl->response;
//        var_dump($res);
//        exit();
        return $res;
    }
}